package feedback

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/response"
	"gitee.com/g-qs/gin-vue-admin/server/model/feedback"
	feedbackReq "gitee.com/g-qs/gin-vue-admin/server/model/feedback/request"
	"gitee.com/g-qs/gin-vue-admin/server/service"
	"gitee.com/g-qs/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type FeedbackApi struct {
}

var ffService = service.ServiceGroupApp.FeedbackServiceGroup.FeedbackService
var UserService = service.ServiceGroupApp.UserServiceGroup.UserService

// CreateFeedback 提交建议
// @Tags Feedback
// @Summary 创建Feedback
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body feedback.Feedback true "提交建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /feedback/createFeedback [post]
func (ffApi *FeedbackApi) CreateFeedback(c *gin.Context) {
	var ff feedback.Feedback
	err := c.ShouldBindJSON(&ff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Title":   {utils.NotEmpty()},
		"Content": {utils.NotEmpty()},
	}
	if err := utils.Verify(ff, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	uid := utils.GetUserID(c)
	status := false
	adoptStatus := false
	data := &feedback.Feedback{
		Title:       ff.Title,
		Content:     ff.Content,
		Img:         ff.Img,
		UID:         &uid,
		Status:      &status,
		AdoptStatus: &adoptStatus,
	}
	if err := ffService.CreateFeedback(data); err != nil {
		global.GVA_LOG.Error("提交失败!", zap.Error(err))
		response.FailWithMessage("提交失败", c)
	} else {
		response.Result(200, "", "提交成功", c)
	}
}

// DeleteFeedback 删除建议
// @Tags Feedback
// @Summary 删除建议
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body feedback.Feedback true "删除建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /feedback/deleteFeedback [delete]
func (ffApi *FeedbackApi) DeleteFeedback(c *gin.Context) {
	var ff feedback.Feedback
	err := c.ShouldBindJSON(&ff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := ffService.DeleteFeedback(ff); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFeedbackByIds 批量删除建议
// @Tags Feedback
// @Summary 批量删除建议
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /feedback/deleteFeedbackByIds [delete]
func (ffApi *FeedbackApi) DeleteFeedbackByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := ffService.DeleteFeedbackByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFeedback 回复建议
// @Tags Feedback
// @Summary 更新Feedback
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body feedback.Feedback true "回复建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /feedback/updateFeedback [put]
func (ffApi *FeedbackApi) UpdateFeedback(c *gin.Context) {
	var ff feedback.Feedback
	err := c.ShouldBindJSON(&ff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Reply":     {utils.NotEmpty()},
		"Reply_img": {utils.NotEmpty()},
	}
	if err := utils.Verify(ff, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := ffService.UpdateFeedback(ff); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindFeedback 用id查询建议
// @Tags Feedback
// @Summary 用id查询Feedback
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query feedback.Feedback true "用id查询建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /feedback/findFeedback [get]
func (ffApi *FeedbackApi) FindFeedback(c *gin.Context) {
	var ff feedback.Feedback
	err := c.ShouldBindQuery(&ff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reff, err := ffService.GetFeedback(ff.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reff": reff}, c)
	}
}

// GetFeedbackList 分页获取Feedback列表
// @Tags Feedback
// @Summary 分页获取Feedback列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query feedbackReq.FeedbackSearch true "分页获取Feedback列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /feedback/getFeedbackList [get]
func (ffApi *FeedbackApi) GetFeedbackList(c *gin.Context) {
	var pageInfo feedbackReq.FeedbackSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := ffService.GetFeedbackInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// FindFeedbackByUid 用户id查询建议
// @Tags Feedback
// @Summary 用户id查询建议
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query feedback.Feedback true "用户id查询建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /feedback/findFeedbackByUid [get]
func (ffApi *FeedbackApi) FindFeedbackByUid(c *gin.Context) {
	uid := utils.GetUserID(c)
	if result, err := ffService.GetFeedbackByUid(uid); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, result, "获取成功", c)
	}
}

// GetFeedbackListByUid 获取建议信箱列表
// @Tags Feedback
// @Summary 获取建议信箱列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query uid true "获取建议信箱列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /feedback/getFeedbackListByUid [get]
func (ffApi *FeedbackApi) GetFeedbackListByUid(c *gin.Context) {
	uid := utils.GetUserID(c)
	if list, err := ffService.GetFeedbackInfoListByUid(uid); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, list, "获取成功", c)
	}
}

// AdoptFeedback 采纳建议
// @Tags Feedback
// @Summary 获取建议信箱列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query uid true "采纳建议"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /feedback/adoptFeedback [put]
func (ffApi *FeedbackApi) AdoptFeedback(c *gin.Context) {
	//ID := c.Query("ID")
	//uid := c.Query("uid")
	//fmt.Println(ID)
	//userID, _ := strconv.ParseUint(uid, 10, 0)
	var ff feedback.Feedback
	err := c.ShouldBindJSON(&ff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := ffService.AdoptFeedback(ff.ID, *ff.UID); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}
